package wyh.controller.Patient;

import com.fasterxml.jackson.databind.ObjectMapper;
import wyh.pojo.Patient;
import wyh.service.PatientService;
import wyh.vo.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/Patient/delList")
public class DeleteListServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("调用了/delList");

        ObjectMapper ojmapper = new ObjectMapper();
        List<Patient> list = ojmapper.readValue(req.getInputStream(), ojmapper.getTypeFactory().constructCollectionType(List.class, Patient.class));
        PatientService service = new PatientService();
        int a = 0;
        for (Patient u : list) {
            if (!service.ServicedeleteById(u.getPatientId())) {
                a++;
            }
        }
        Result result = null;
        if (a == 0) {
            result = new Result(200, "全部成功删除", null);
        } else {
            result = new Result(500, "有" + a + "条失败", null);
        }
        String string = ojmapper.writeValueAsString(result);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(string);
    }
}
